#include <mips/regdef.h>

#define V_ERR_MSG	4188	
#define V_TEXT		4184	

#define V_SSIZE		4184	

#define V_RA			4176
#define	V_FP			4172
#define V_GP			4168
#define V_INDEX			4160
#define V_RESULT		4156

#define V_TAG_COUNT		4152
#define V_LAST_POSITION	4148
#define V_START_INDEX	4144
#define V_TAGS			40
#define V_LOCAL_ERR_MSG 24
#define V_LOCAL_TAGS    20
#define V_LOCAL_TAG_COUNT 16

	.text
	.align	2
	.globl	validate
	.ent	validate
validate:
	.frame	$fp,V_SSIZE,ra	
	.mask	0xd0000000,-8
	.fmask	0x00000000,0
	.set	noreorder
	.cpload	t9
	.set	reorder
	subu	sp,sp,V_SSIZE
	.cprestore 32
	sw	ra,V_RA(sp)
	sw	$fp,V_FP(sp)
	sw	gp,V_GP(sp)
	move	$fp,sp
	sw	a0,V_TEXT($fp)
	sw	a1,V_ERR_MSG($fp)
	sw	zero,V_TAGS($fp)
	sw	zero,V_START_INDEX($fp)
	sw	zero,V_LAST_POSITION($fp)
	sw	zero,V_TAG_COUNT($fp)
	addu	v1,$fp,V_LAST_POSITION
	addu	a3,$fp,48
	addu	v0,$fp,V_TAG_COUNT
	sw	v0,V_LOCAL_TAG_COUNT(sp)
	addu	v0,$fp,V_TAGS
	sw	v0,V_LOCAL_TAGS(sp)
	lw	v0,V_ERR_MSG($fp)
	sw	v0,V_LOCAL_ERR_MSG(sp)
	lw	a0,V_TEXT($fp)
	lw	a1,V_START_INDEX($fp)
	move	a2,v1
	la	t9,findTag
	jal	ra,t9
	sw	v0,V_RESULT($fp)
	sw	zero,V_INDEX($fp)
LOOP:
	lw	v0,V_INDEX($fp)
	lw	v1,V_TAG_COUNT($fp)
	slt	v0,v0,v1
	bne	v0,zero,LIBERAR_TAGS
	b	SALIR
LIBERAR_TAGS:
	lw	v0,V_INDEX($fp)
	sll	v1,v0,2
	addu	v0,$fp,V_TAGS
	addu	v0,v1,v0
	addu	v0,v0,8
	lw	a0,0(v0)
	la	t9,free
	jal	ra,t9
	lw	v0,V_INDEX($fp)
	addu	v0,v0,1
	sw	v0,V_INDEX($fp)
	b	LOOP
SALIR:
	lw	v0,V_RESULT($fp)
	move	sp,$fp
	lw	ra,V_RA(sp)
	lw	$fp,V_FP(sp)
	addu	sp,sp,V_SSIZE
	j	ra
	.end	validate
